package com.zcah.db.dbsource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.zcah.db.DbSource;
import com.zcah.db.dbsource.impl.SqlServerSource;
import com.zcah.db.dbsource.impl.OracleSource;
import com.zcah.db.dbsource.impl.PostgreSource;
import com.zcah.db.domain.DbsourceDO;

/**
 * 工厂类，获取指定数据源
 * 
 * @author ke
 *
 */
@Component
public class DbSourceFactory {
	
	private static final Logger logger = LoggerFactory.getLogger(DbSourceFactory.class);
	
	private DbSourceFactory() {
	}

	public static DbSource getInstance(DbsourceDO db) {
		switch (db.getDbName()) {
		case DbType.ORACLE:
			return new OracleSource(db);
		case DbType.SQL_SERVER:
			return new SqlServerSource(db);
		case DbType.POSTGRESQL:
			return new PostgreSource(db);
		default:
			throw new IllegalArgumentException("非法数据库类型：" + db.getDbName());
		}
	}
	

	// 此处统一加载数据库驱动
	static {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (ClassNotFoundException e) {
			logger.error(e.getLocalizedMessage(), e);
			System.exit(-1);
		}
	}
	
}
